package com.library.workbench.web.controller.user;

import com.library.commons.constant.Constant;
import com.library.settings.pojo.User;
import com.library.workbench.pojo.Borrow;
import com.library.workbench.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BorrowInfoController {
    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/workbench/user/borrowAndReturnInfo/toBorrowInfo.do")
    public String toBorrowInfo(){
        return "workbench/user/borrowAndReturnInfo/information";
    }

    @RequestMapping("/workbench/user/borrowAndReturnInfo/queryAllReturnForPage.do")
    @ResponseBody
    public Object queryAllReturnForPage(HttpSession session,String bookName, Integer pageNo, Integer pageSize){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        // 封装对象
        Map<String,Object> map=new HashMap<>();
        map.put("bookName",bookName);
        map.put("userAccount",user.getUserAccount());
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        // 调用service层方法
        List<Borrow> borrows = borrowService.queryBorrowInfoByConditionForPage(map);
        int rowsTotal = borrowService.queryBorrowInfoByConditionForRows(map);
        map.put("books",borrows);
        map.put("totalRows",rowsTotal);
        // 返回信息
        return map;
    }
}
